<?php

namespace app\web\controller;

use app\web\controller\Base;
use think\Controller;
use think\Db;

class Index extends Base {

    public $sysset;

    public function __construct(\think\App $app = null) {
        parent::__construct($app);
        $conf = Db::name('sysset')->where(['id' => 1])->find();
        $this->sysset = json_decode($conf['sets'], true);
        $this->view->assign('sysset', $this->sysset);
    }

    public function index() {

        $_GPC = input();
        $data = pdo_get('sale', array('id' => $_GPC['id']));
        $msg = '';
        if (intval($data['number']) > 0 && $data['number'] < $data['view']) {
            $msg = '活动已结束';
        }
        if ($data['endtime'] + 86400 < time()) {
            $msg = '活动已结束';
        }
        if ($data['starttime'] > time()) {
            $msg = '活动未开始';
        }
        if (isset($_GPC['code'])) {
            if ($msg == '') {
                pdo_update('sale', array('view' => $data['view'] + 1), array('id' => $data['id']));
                if ($data['number'] > 0) {
                    $member = pdo_get('member', array('id' => $data['member_id']));
                    
                    // if($member['num']-$member['numing']<=0)
                    // {
                    //     exit;
                    // }
                pdo_update('member', array('numing' =>$member['numing'] + 1), array('id' => $member['id']));
                }
            }
            $params = convertUrlQuery($this->request->domain(true) . $_SERVER['REQUEST_URI']);
            unset($params['code']);
            $url = $this->request->domain(true) .'/web?' . http_build_query($params);
            header('Location: ' . $url);
            exit;
        }
        $title = $data['title'];
        $data['poster'] = json_decode($data['poster'], true);
        $data['replace'] = json_decode($data['replace'], true);
        $data['video'] = json_decode($data['video'], true);
        if (isset($data['video']['list']) && is_array($data['video']['list'])) {
            $videoidx = array_rand($data['video']['list']);
            $data['video']['title'] = $data['video']['list'][$videoidx]['title'] ? $data['video']['list'][$videoidx]['title'] : $data['title'];
            $data['video']['path'] = $data['video']['list'][$videoidx]['path'] ? $data['video']['list'][$videoidx]['path'] : '';
        } else {
            $videoidx = 0;
            $data['video']['title'] = isset($data['video']['title']) ? $data['video']['title'] : $data['title'];
            $data['video']['path'] = isset($data['video']['path']) ? $data['video']['path'] : '';
        }
        $data['card'] = json_decode($data['card'], true);
        
        $_W=[
                'baseurl'=>$this->request->domain(true),
                'fans'=>session('fans')
            ];
            
            $this->view->assign('videoidx', $videoidx);
        $this->view->assign('msg', $msg);
        $this->view->assign('data', $data);
        $this->view->assign('title', $title);
        $this->view->assign('data', $data);
        
        return $this->view->fetch($data['template']);
    }

    public function bindmerch() {

        $_GPC = input();
        $msg = '绑定失败';
        if (session('fans')['id']) {
            pdo_update('member', array('ismerch' => 1), array('id' => session('fans')['id']));
            $msg = '绑定成功';
        }
        $this->view->assign('msg', $msg);
        return $this->view->fetch('public/message');
        exit;
    }

    public function getopenid() {

        $_GPC = input();
        $msg = '获取openid失败';
        if (session('fans')['openid']) {
            $msg = 'openid：' . session('fans')['openid'];
        }
        $this->view->assign('msg', $msg);
        return $this->view->fetch('public/message');
        exit;
    }

    public function bindverifyer() {

        $_GPC = input();
        $msg = '绑定失败';
        if (session('fans')['id']) {
            $sale_verifyer = pdo_get('sale_verifyer', array('sale_id' => $_GPC['id'], 'member_id' => session('fans')['id']));
            if (!$sale_verifyer) {
                pdo_insert('sale_verifyer', array('member_id' => session('fans')['id'], 'sale_id' => $_GPC['id'], 'createtime' => time()));
            }
            $msg = '绑定成功';
        }
        $this->view->assign('msg', $msg);
        return $this->view->fetch('public/message');
        exit;
    }

}
